package com.jacen.fzxs.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jacen.fzxs.entity.ImageEntity;
import com.jacen.fzxs.entity.UserEntity;
import com.jacen.fzxs.entity.WallEntity;
import com.jacen.fzxs.service.IImageService;
import com.jacen.fzxs.service.IWallService;

/**
 * 百度地图接入管理
 * @author Jacen
 * 2014年9月12日 下午11:01:34
 */
@Controller
public class MapController extends BaseController {
	
	@Resource
	private IWallService wallService;
	@Resource
	private IImageService imageService;
	
	private static Logger logger = LoggerFactory.getLogger(MapController.class);
	
	/**
	 * 查看地图
	 * @param request
	 * @param modelMap
	 * @return String
	 */
	@RequestMapping("/map.do")
	public String getMap(HttpServletRequest request,ModelMap modelMap){
		List<WallEntity> list = wallService.getAll();
		List<ImageEntity> images = new ArrayList<ImageEntity>();
		long start = System.currentTimeMillis();
		logger.info("====开始循环查找图片：===="+start);
		for(int i=0; i< list.size();i++){
			images = imageService.getList(list.get(i).getId());
			if(images.size()==0)continue;
			list.get(i).setImages(getImageString(images));
		}
		long end = System.currentTimeMillis();
		logger.info("====耗时：===="+(end-start)/1000 +"s");
		modelMap.addAttribute("list", list);
		UserEntity user = getLocalUser(request);
		logger.info("用户："+user.getUserName()+"查看地图");
		return "map";
	}
	/**
	 * 拼接图片字符串
	 * @param list
	 * @return
	 */
	private String getImageString(List<ImageEntity> list){
		StringBuffer sb = new StringBuffer();
		for(ImageEntity entity : list){
			sb.append(entity.getUrl()+",");
		}
		return sb.toString().substring(0, sb.toString().length()-1);
	}

}
